package com.aoxin.controller.course.systemCourse;

import com.aoxin.model.course.AloneCourseToPage;
import com.aoxin.service.inter.course.systemCourse.AloneCourseToPageService;
import com.aoxin.util.AjaxRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.Map;

@RestController
@RequestMapping("/web/system/")
public class AloneCourseToPageController {
    @Autowired
    private AloneCourseToPageService AloneCourseToPageService;

    /**
     * 给某个包插入课程
     * @param courseIds
     * @param coursePageId
     * @return
     */
    @PostMapping("insertAloneCourseToPage")
    public AjaxRes insertAloneCourseToPage(@RequestParam("courseIds") Integer courseIds[], Integer coursePageId, AjaxRes res) {

        try {
            AloneCourseToPageService.deleteCourseToPage(null,coursePageId);
            Integer number = AloneCourseToPageService.insertCourseToPage(courseIds, coursePageId);
            res.setSucceed(number);
        } catch (Exception e) {
            res.setFailMsg(e.getMessage());
        }
        return res;
    }

    /**
     * 根据课程包id和学校查询对应课程
     * @param AloneCourseToPage
     * @return
     */
    @GetMapping("getCourseToPage")
    public AjaxRes getCourseToPage(AloneCourseToPage AloneCourseToPage, AjaxRes res) {
        try {
            LinkedList<Map<String, Object>> list = AloneCourseToPageService.getCourseToPage(AloneCourseToPage);
            res.setSucceed(list);
        } catch (Exception e) {
            res.setFailMsg(e.getMessage());
        }
        return res;
    }

    /**
     * 修改课程包里的课程
     *
     * @param AloneCourseToPage
     * @param res
     * @return
     */
    @PutMapping("updateCourseToPage")
    public AjaxRes updateCourseToPage(AloneCourseToPage AloneCourseToPage, AjaxRes res) {
        try {
            Integer col = AloneCourseToPageService.updateCourseToPage(AloneCourseToPage);
            res.setSucceed(col);
        } catch (Exception e) {
            res.setFailMsg(e.getMessage());
        }
        return res;
    }

    /**
     * 删除课程包里的课程
     * @param res
     * @param csToPgId
     * @return
     */
    @DeleteMapping("deleteCourseToPage")
    public AjaxRes deleteCourseToPage(AjaxRes res, Integer csToPgId,Integer coursePageId) {
        try {
            res.setSucceed(AloneCourseToPageService.deleteCourseToPage(csToPgId,coursePageId));
        } catch (Exception e) {
            res.setFailMsg(e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 获取未添加到课程包的课程
     * @param res
     * @param coursePageId
     * @return
     */

    @GetMapping("getOtherCourseToPage")
    public AjaxRes getOtherCourseToPage(AjaxRes res, Integer coursePageId) {
        try {
            res.setSucceed(AloneCourseToPageService.getOtherCourseToPage(coursePageId));
        } catch (Exception e) {
            res.setFailMsg(e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

}
